package com.victor.bookretail.common;

import com.victor.bookretail.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public AppResponse<Object> BusinessExceptionHandler(BusinessException ex) {
        return AppResponse.error(ex.getResultCode());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public AppResponse<Object> argumentNotValidExceptionHandler(MethodArgumentNotValidException ex) {
        log.warn("API parameter not valid, exception:", ex);
        return AppResponse.error(ResultCode.PARAMETER_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public AppResponse<Object> otherExceptionHandler(Exception ex) {
        log.error("Other exception occurred, exception:", ex);
        return AppResponse.error(ResultCode.UNKNOWN_ERROR);
    }

}
